<template>
    <!-- 优势组件 -->
    <section
        :style="{
            paddingTop: itemConfig.paddingTop + 'px',
            paddingBottom: itemConfig.paddingBottom + 'px',
            marginTop: itemConfig.marginTop + 'px',
            marginBottom: itemConfig.marginBottom + 'px',
            background: `linear-gradient(
                ${itemConfig.bgEdg}deg,
                ${itemConfig.bgColorOne},
                ${itemConfig.bgColorTow},
                ${itemConfig.bgColorThree})`
        }"
    >
        <div class="max-w-[1200px] m-auto px-5">
            <div
                v-if="itemConfig.title || itemConfig.intro"
                class="flex flex-col gap-1.5"
                :style="{ marginBottom: itemConfig.headerBottom + 'px' }"
            >
                <h3
                    v-if="itemConfig.title"
                    class="text-center text-7xl font-bold"
                    :style="{ color: itemConfig.titleColor }"
                >{{ itemConfig.title }}</h3>

                <p
                    v-if="itemConfig.intro"
                    class="text-center text-lg"
                    :style="{ color: itemConfig.introColor }"
                >{{ itemConfig.intro }}</p>
            </div>

            <div
                class="grid gap-6"
                :style="{
                    'grid-template-columns': `repeat(${itemConfig.rows}, minmax(0, 1fr))`
                }"
            >
                <template v-for="(item, index) in (itemData as itemDataType[])">
                    <div
                        v-if="item.isEnable"
                        :key="index"
                        class="card rounded-xl shadow-lighter"
                        :style="{ backgroundColor: item.bgColor }"
                        @click="handleJump(item, 'card')"
                    >
                        <div v-if="item.icon" class="w-14 h-14 mb-5">
                            <el-image
                                :src="appStore.toAbsoluteUrl(item.icon)"
                                class="w-full h-full rounded-lg"
                            />
                        </div>

                        <h4
                            class="font-bold text-2xl mb-3"
                            :style="{ color: item.nameColor }"
                        >
                            {{ item.name }}
                        </h4>

                        <p :style="{ color: item.descColor }">
                            {{ item.desc }}
                        </p>

                        <nuxt-link
                            v-if="item.more"
                            class="cursor-pointer mt-5"
                            :style="{ color: item.moreColor }"
                            @click="handleJump(item)"
                        >
                            {{ item.more }}
                        </nuxt-link>
                    </div>
                </template>
            </div>
        </div>
    </section>
</template>

<script setup lang="ts">
import { popupEnum } from '~/enums/app'
import useAppStore from '~/stores/app'
import useUserStore from '~/stores/user'
import validateUtil from '~/utils/validate'

interface itemConfigType {
    title: string;
    intro: string;
    rows: string;
    paddingTop: number;
    paddingBottom: number;
    marginTop: number;
    marginBottom: number;
    headerBottom: number;
    bgEdg: number;
    bgColorOne: string;
    bgColorTow: string;
    bgColorThree: string;
    titleColor: string;
    introColor: string;
}

interface itemDataType {
    icon: string;
    name: string;
    desc: string;
    more: string;
    nameColor: string;
    descColor: string;
    moreColor: string;
    bgColor: string;
    isLogin: boolean;
    isEnable: boolean;
    link: any;
}

withDefaults(
    defineProps<{
        itemConfig?: itemConfigType,
        itemData?: itemDataType[]
    }>(),
    {
        itemConfig: {} as itemConfigType | any,
        itemData: [] as itemDataType[] | any
    }
)

const router = useRouter()
const appStore = useAppStore()
const userStore = useUserStore()

const handleJump = (item: itemDataType, type?: string) => {
    if (type === 'card' && item.more) {
        return
    }

    if (!userStore.isLogin && item.isLogin) {
        return appStore.setPopup(popupEnum.LOGIN)
    }

    if (item.link?.path) {
        if (validateUtil.isExternal(item.link?.path)) {
            window.open(item.link?.path, '_blank')
        } else {
            router.push({
                path: item.link?.path,
                query: item.link?.query
            })
        }
    }
}
</script>

<style scoped lang="scss">
.card {
    display: flex;
    flex-direction: column;
    align-items: center;
    padding: 40px;
    text-align: center;
    cursor: pointer;
    transition: all 0.5s;
    &:hover {
        transform: translateY(-10px);
    }
}
</style>
